/**
 * Copyright (C) BFH www.bfh.ch 2011
 * Code written by: Patrick Dobler, Marc Folly
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.ti.kybernetik.gui.slick.components;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import javax.vecmath.Point2d;
import javax.vecmath.Vector2d;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Circle;
import org.newdawn.slick.geom.Polygon;

import ch.bfh.ti.kybernetik.engine.VecMathUtils;
import ch.bfh.ti.kybernetik.engine.controller.roboter.RoboterConstruction;
import ch.bfh.ti.kybernetik.engine.model.LightSensor;
import ch.bfh.ti.kybernetik.engine.model.Motor;
import ch.bfh.ti.kybernetik.engine.model.Roboter;
import ch.bfh.ti.kybernetik.engine.model.RoboterElement;
import ch.bfh.ti.kybernetik.gui.slick.SlickRenderUtils;

/**
 * This Class renders a {@link Roboter} instance
 * 
 */
public class RoboterComponent implements SelectableSlickComponent<Roboter> {

	private Roboter roboter;

	private RoboterConstruction roboterConstruction;

	private boolean renderViewArea = true;

	private boolean renderRoboter = true;;

	private final double directionPointLength = 100;

	private Color color;

	private Set<RoboterElement> hightLightRoboterModelElements = new HashSet<RoboterElement>();

	private boolean selected = false;

	public RoboterComponent(Roboter roboter, RoboterConstruction roboterConstruction) {
		this.roboter = roboter;
		this.roboterConstruction = roboterConstruction;
		Random numGen = new Random();
		Color c = new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
		this.setColor(c);
	}

	@Override
	public void render(GameContainer gc, final Graphics g) throws SlickException {
		this.renderCurrentRoboter(g);
	}

	public boolean intersectWithPoint(Point2d point) {
		Circle c = SlickRenderUtils.getCircle(roboter.getX(), roboter.getY(), 20);
		return c.contains((float) point.getX(), (float) point.getY());
	}

	private void renderCurrentRoboter(Graphics g) {
		LightSensor leftLightSensor = roboter.getLeftLightSensor();
		LightSensor rightLightSensor = roboter.getRightLightSensor();
		Vector2d directionVector = roboter.getDirection();
		if (renderRoboter) {
			g.setColor(color);
			SlickRenderUtils.fillCircle(g, roboter.getX(), roboter.getY(), 3);
			if (selected) {
				g.setColor(Color.yellow);
				SlickRenderUtils.drawCircle(g, roboter.getX(), roboter.getY(), 4);
				SlickRenderUtils.drawCircle(g, roboter.getX(), roboter.getY(), 5);
				SlickRenderUtils.drawCircle(g, roboter.getX(), roboter.getY(), 6);
			}
			g.setColor(color);
			double currentDirectionPointX = roboter.getX() + directionPointLength * directionVector.getX();
			double currentDirectionPointY = roboter.getY() + directionPointLength * directionVector.getY();
			g.drawLine((float) roboter.getX(), (float) roboter.getY(), (float) currentDirectionPointX, (float) currentDirectionPointY);

			drawMotor(g, roboter.getLeftMotor());
			drawMotor(g, roboter.getRightMotor());

			drawLightSensor(g, leftLightSensor);

			drawLightSensor(g, rightLightSensor);
		}

		if (renderViewArea) {
			// Left Light Sensor View
			g.setColor(SlickRenderUtils.getAlphaColor(Color.orange, 1.0f));
			drawLightSensorViewField(g, leftLightSensor, directionVector, roboterConstruction);

			// Right Light Sensor View
			g.setColor(Color.green);
			drawLightSensorViewField(g, rightLightSensor, directionVector, roboterConstruction);
		}

	}

	private void drawLightSensor(Graphics g, LightSensor lightSensor) {
		g.setColor(Color.red);
		if (this.hightLightRoboterModelElements.contains(lightSensor)) {
			g.setColor(Color.white);
		}
		Point2d leftLightSensorPoint = roboterConstruction.getRoboterElementPoint(lightSensor);
		SlickRenderUtils.fillCircle(g, leftLightSensorPoint.getX(), leftLightSensorPoint.getY(), 3);
	}

	private void drawMotor(Graphics g, Motor motor) {
		Point2d moterLeftPoint = roboterConstruction.getRoboterElementPoint(motor);
		g.setColor(Color.yellow);
		SlickRenderUtils.fillCircle(g, moterLeftPoint.getX(), moterLeftPoint.getY(), 3);
	}

	public void hightLightRoboterElement(RoboterElement roboterElement) {
		this.hightLightRoboterModelElements.add(roboterElement);
	}

	public void unHightLightRoboterElement(RoboterElement roboterElement) {
		this.hightLightRoboterModelElements.remove(roboterElement);
	}

	private void drawLightSensorViewField(Graphics g, final LightSensor sensor, Vector2d directionVector,
			RoboterConstruction roboterConstruction) {
		Point2d lightSensorPoint = roboterConstruction.getRoboterElementPoint(sensor);

		Vector2d vec1 = roboterConstruction.getRoboterElementAngleVector(sensor);
		Point2d viewDirectionPoint = VecMathUtils.getScaledVectorPoint(vec1, lightSensorPoint, 50);
		String strring = "" + (int) sensor.getSensorIntensity();

		Point2d viewDirectionPoint2 = VecMathUtils.getScaledVectorPoint(vec1, lightSensorPoint, 52);
		g.drawString(strring, (float) viewDirectionPoint2.getX(), (float) viewDirectionPoint2.getY());

		double angleMinMax = sensor.getViewFieldSize() / 2;
		Vector2d vecMinViewField = VecMathUtils.rotateVector(vec1, angleMinMax);
		Vector2d vecMaxViewField = VecMathUtils.rotateVector(vec1, -angleMinMax);
		Point2d vecMinViewFieldPoint = VecMathUtils.getScaledVectorPoint(vecMinViewField, lightSensorPoint, 300);
		Point2d vecMaxViewFieldPoint = VecMathUtils.getScaledVectorPoint(vecMaxViewField, lightSensorPoint, 300);
		// drawLine(lightSensorPoint, vecMinViewFieldPoint, g);
		// drawLine(lightSensorPoint, vecMaxViewFieldPoint, g);

		Polygon pol = new Polygon();
		pol.addPoint((float) lightSensorPoint.getX(), (float) lightSensorPoint.getY());
		pol.addPoint((float) vecMaxViewFieldPoint.getX(), (float) vecMaxViewFieldPoint.getY());
		pol.addPoint((float) vecMinViewFieldPoint.getX(), (float) vecMinViewFieldPoint.getY());

		// GradientFill fill = new GradientFill((float) lightSensorPoint.getX(),
		// (float) lightSensorPoint.getY(), Color.lightGray,
		// (float) viewDirectionPoint.getX(), (float) viewDirectionPoint.getY(),
		// Color.black);
		// g.draw(pol, fill);
		SlickRenderUtils.drawLine(lightSensorPoint, vecMaxViewFieldPoint, g);
		SlickRenderUtils.drawLine(lightSensorPoint, vecMinViewFieldPoint, g);
		SlickRenderUtils.drawLine(lightSensorPoint, viewDirectionPoint, g);

		SlickRenderUtils.drawArcBetweenPoints(g, lightSensorPoint, vecMinViewFieldPoint, vecMaxViewFieldPoint);

	}

	public RoboterConstruction getRoboterConstruction() {
		return roboterConstruction;
	}

	public void setRenderViewArea(boolean renderViewArea) {
		this.renderViewArea = renderViewArea;
	}

	public boolean isRenderViewArea() {
		return renderViewArea;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	public boolean isSelected() {
		return selected;
	}

	@Override
	public void setX(double X) {
		this.roboter.setX(X);
	}

	@Override
	public void setY(double Y) {
		this.roboter.setY(Y);
	}

	@Override
	public Roboter getModelObject() {
		return roboter;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public Color getColor() {
		return color;
	}

}
